package cn.jx.asc.lhg.bzxq.utils;

import android.util.Xml;

import cn.jx.asc.lhg.bzxq.model.UserBase;
import org.xmlpull.v1.XmlPullParser;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/26.
 */

public class Http {
    public static String p = "http://211.159.186.83:8080/BZXQServer/servlet/";
    public static List<UserBase> getUser(String path) throws Exception {
        List<UserBase> list = new ArrayList<UserBase>();
        URL url = new URL(p+path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(5000);
        int code = conn.getResponseCode();
        if (code == 200) {
            InputStream is = conn.getInputStream();
            XmlPullParser parser = Xml.newPullParser();
            parser.setInput(is, "utf-8");
            int eventType = parser.getEventType();
            UserBase userBase = null;
            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_TAG:
                        if("item".equals(parser.getName())){
                            userBase = new UserBase();
                        }else if("name".equals(parser.getName())){
                            userBase.setName(parser.nextText());
                        }else if("fen".equals(parser.getName())){
                            userBase.setFen(parser.nextText());
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if("item".equals(parser.getName())){
                            list.add(userBase);
                        }
                        break;
                    default:{

                    }
                }
                eventType = parser.next();
            }
            return list;
        } else {
            return null;
        }
    }

    public static String sendMessage(Map<String,String> params, String bianma, String path){
        URL url;
        StringBuffer buf = new StringBuffer();
        if(params!=null &&!params.isEmpty()){
            buf.append(p+path);
            for(Map.Entry<String, String> a:params.entrySet()){
                buf.append(a.getKey()).append("=").append(URLEncoder.encode(a.getValue())).append("&");
            }
            buf.deleteCharAt(buf.length()-1);

        }
        System.out.println("buf:"+buf.toString());
        try {
            url = new URL(buf.toString());
            if(url!= null){
                HttpURLConnection http = (HttpURLConnection) url.openConnection();
                if(http==null){
                    return "-1";
                }
                http.setConnectTimeout(3000);
                http.setRequestMethod("POST");
                http.setDoInput(true);
                http.setDoOutput(true);

                http.setRequestProperty("Content-type","application/x-www-form-urlencoded");

                OutputStream output = http.getOutputStream();
                output.close();

                int code = http.getResponseCode();
                if(code == 200){
                    return changInputStream(http.getInputStream(),bianma);
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "-1";
    }
    public static String changInputStream(InputStream inputStream, String bianma){
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] data = new byte[1024];
        int len = 0;
        String result = "";
        if (inputStream != null)
        {
            try {
                while ((len = inputStream.read(data)) != -1)
                {
                    outputStream.write(data, 0, len);
                }
                result = new String(outputStream.toByteArray(), bianma);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return result;
    }


}
